package action;

import java.text.SimpleDateFormat;
import java.util.List;

import com.opensymphony.xwork2.ModelDriven;

import entity.Students;

import seivice.impl.StudentsDAOImpl;
import service.StudentsDAO;

/**
 * 学生Action类
 * @author 请叫我小纯洁丶
 *
 */
public class StudentsAction extends SuperAction implements ModelDriven<Students>  {

	/*
	 * 模型驱动方式 须实现 implements ModelDriven<Students> 接口
	 */
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Students s = new Students();
	
	//查询所有学生的动作
	public String query(){
		
		StudentsDAO sdao = new StudentsDAOImpl();
		List<Students> list = sdao.queryAllStudents();
		//放入session
		if(list!=null && list.size()>0){
			session.setAttribute("students_list",list);
		}
		return "query_success";
	}

	//删除学生动作
	public String delete(){
		
		StudentsDAO sdao = new StudentsDAOImpl();
		String sid = request.getParameter("sid");
		sdao.deleteStudents(sid);
		return "delete_success";
	}

	//添加学生动作(模型驱动方法)
	public String add(){
		StudentsDAO sdao = new StudentsDAOImpl();
		sdao.addStudents(s);
		return "add_success";
	}
	
	
//	//添加学生
//    public String add() throws Exception{
//         Students s = new Students();
//         s.setSname(request.getParameter("sname"));
//         s.setGender(request.getParameter("gender"));
//         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//         s.setBirthday(sdf.parse(request.getParameter("birthday")));
//         s.setAddress(request.getParameter("address"));
//         StudentsDAO sdao = new StudentsDAOImpl();
//         sdao.addStudents(s);
//         return "add_success";
//    }
	
	//修改学生动作
	public String modify(){
		
		//获得传递过的学生ID
		String sid = request.getParameter("sid");
		StudentsDAO sdao =  new StudentsDAOImpl();
		Students s = sdao.queryStudentsBySid(sid);
		//保存到session
		session.setAttribute("modify_students", s);
		return "modify_success";
 	}
	
	//保存修改后学生动作
	public String save() throws Exception{
		Students s  = new Students();
		s.setSid(request.getParameter("sid"));
		s.setSname(request.getParameter("sname"));
		s.setGender(request.getParameter("gender"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		s.setBirthday(sdf.parse(request.getParameter("birthday")));
		s.setAddress(request.getParameter("address"));
		StudentsDAO sdao = new StudentsDAOImpl();
		sdao.updateStudents(s);	
		return "save_success"; 
	}
	@Override
	public Students getModel() {
		// TODO Auto-generated method stub
		return this.s;
	}
}
